classdef Chebyshev1 < FilterFather
    properties
        NormalizedPoles = [];
    end
    methods
        
        function this = Chebyshev1(guiData)
            this.calculateFilter(guiData);
            minEdge = min([guiData.Fp guiData.Fa])*2*pi;
            maxEdge = max([guiData.Fp guiData.Fa])*2*pi;
            this.calcData([minEdge maxEdge]);
        end
        
        function calculateFilter(obj,guiData)
            Aa = guiData.Aa;
            Ap = guiData.Ap;
            obj.Approximation = 'Chebyshev I';
            obj.FilterType = guiData.filterType;
            Wp = guiData.Fp*2*pi;
            Wa = guiData.Fa*2*pi;
            [Wp Wa] = obj.adjustBand(Wp,Wa);
            obj.DesnormValue = guiData.DenormVal;
            obj.DesnormType = guiData.DenormType;
            fixOrder = guiData.fixOrder;
            MaxOrder = guiData.order;
            obj.normalization(Wp,Wa);
            
            epsilon = sqrt(10^(.1*abs(Ap))-1);

            % Order calculus
            N = ceil(acosh( sqrt(10.^(.1*abs(Aa))-1)./epsilon ) /acosh(obj.WaNormalized));
           
            if MaxOrder == 0
                obj.Order = N;
            else 
                if(N > MaxOrder || fixOrder == true)
                    N = MaxOrder;
                end
                obj.Order = N;
            end
            
            
%             if obj.Order > MaxOrder
%                 obj.Order = MaxOrder;
%             end
            
            
            
            a = sinh(1/N*asinh(1/epsilon));
            b = cosh(1/N*asinh(1/epsilon));
            x = -1*a*sin(pi*(1:2:N-1)/(2*N));
            y = b*cos(pi*(1:2:N-1)/(2*N));
            p = x + 1i*y;
            p = [p, conj(p)]; % To sort numerical errors
            p = p(:);
            if rem(N,2)==1   % N is odd
                p = [p; -a];    % The negative real pole
            end
            obj.NormalizedPoles = p;
            obj.Poles = p;
            obj.Zeros = [];
            obj.K = abs(real(prod(obj.Poles)));
            if rem(obj.Order,2) == 0 %Si es impar la normalizada se encuentra en -Ap, la debo levantar
                obj.K = obj.K*10^(-Ap/20);
            end
            obj.frecDesnorm(Aa);
            obj.K = abs(real(prod(obj.Poles)));           
            obj.desnormalization();
            if(rem(obj.Order,2)==0)
                obj.TransferFunction = obj.TransferFunction*10^(-Ap/20);
            end
            obj.TransferFunction = obj.TransferFunction*10^(guiData.gain/20);
        end
    end
end